package com.youi.third.meiyijiauser;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youi.entity.User;
import com.youi.entity.Userwechat;
import com.youi.exception.BusinessException;
import com.youi.third.meiyijiauser.model.AccessToken;
import com.youi.third.meiyijiauser.model.MemberInfo;
import com.youi.third.meiyijiauser.model.MemberRequest;
import com.youi.third.meiyijiauser.model.MemberResult;
import com.youi.third.meiyijiauser.model.RegmenberRequest;
import com.youi.util.CommonInfoUtil;
import com.youi.util.HttpUtil;
import com.youi.util.JsonUtil;

/**
 * 美宜佳会员接口工具
 * @author XJY
 *
 */
public class MeyijiauserUtil {

	/**
	 * 合作商名称
	 */
	private final static String PartnersName="美宜佳健康"; 
	
	/**
	 * 合作商编号
	 */
	private final static String PartnersNo="myjhealth";
			
	/**
	 *密钥 
	 */
	private final static String Secretkey="9544470189a64692a91d5a9af0a6e739";
	
	
	/**
	 * 获取accessToken
	 * @return
	 */
	public static String  getAccessToken() {
		Map<String, String> prams=new HashMap<String, String>();
		//固定值：client_credential
		prams.put("grant_type", "client_credential");
		//合作商编号
		prams.put("partner_code", PartnersNo);
		//合作商密钥
		prams.put("secret", Secretkey);
		String result=HttpUtil.MeiYiJiaAction("https://servicedx.myj.com.cn/mp_token.ashx?"+HttpUtil.getUrlParamsFromMap(prams), HttpUtil.GET, null, null);
		AccessToken accessToken=JsonUtil.fromJson(result, AccessToken.class);
		return accessToken.getResult().getAccess_token();
	}
	
	/**
	 * 获取微信信息
	 * @param userwechat
	 * @throws BusinessException 
	 */
	public static MemberResult  getUserInfo(Userwechat userwechat,String accessToken) throws BusinessException {
		MemberRequest memberRequest= new MemberRequest(userwechat.getUnionid(), "wxuid");
		JSONObject prams=new JSONObject();
		//固定值：client_credential
		prams.put("token", accessToken);
		//合作商编号
		prams.put("interface", "MyjCrm.Service.GetMemberInfo");
		//合作商密钥
		prams.put("biz",JsonUtil.toJson(memberRequest));
		String action=HttpUtil.Action("https://servicedx.myj.com.cn/mp_aip.ashx", prams.toJSONString(),HttpUtil.ContentType2, null);
		MemberInfo memberInfo=JsonUtil.fromJson(action, MemberInfo.class);
		Object object=JsonUtil.fromJson(memberInfo.getResult(), Object.class);
		JSON json=(JSON) JSON.toJSON(object);
		MemberResult memberResult=JSONObject.toJavaObject(json, MemberResult.class);	
		return memberResult;
	}
	
	/**
	 * 注册会员
	 * @param memberResult 
	 * @return
	 * @throws BusinessException 
	 */
	public static MemberResult regMember(User user,Userwechat userwechat,String accessToken) {
		if (user.getPhone()!=null&&user.getPhone().length()>0) {
			RegmenberRequest regmenberRequest=new RegmenberRequest("wxuid", user.getPhone(), userwechat.getUnionid(), null, user.getNickName(), user.getImg(), null);
			JSONObject prams=new JSONObject();
			//固定值：client_credential
			prams.put("token", accessToken);
			//合作商编号
			prams.put("interface", "MyjCrm.Service.RegMember");
			//合作商密钥
			prams.put("biz",JsonUtil.toJson(regmenberRequest));
			String result=HttpUtil.Action("https://servicedx.myj.com.cn/mp_aip.ashx", prams.toJSONString(),HttpUtil.ContentType2, null);		
			MemberInfo memberInfo=JsonUtil.fromJson(result, MemberInfo.class);
			MemberResult memberResult=JsonUtil.fromJson(memberInfo.getResult(),MemberResult.class);
			return memberResult;
		}
		return null;
	}

}
